
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

char buf[1024];

int main()
{
  int fd;
  char option;

  printf("*********************************\n");
  fd = open("/dev/my_device", O_RDWR);
  if(fd < 0) {
    printf("Failed open device file\n");
    return 0;
  }

  while(1) {
    printf("****** Choose the Option ********\n");
    printf("        (1) Write                \n");
    printf("        (2) Read                 \n");
    printf("        (3) Exit                 \n");
    printf("*********************************\n");
    scanf(" %c", &option);

    switch(option) {
      case '1':
        printf("Enter the data to write:");
        scanf("  %[^\t\n]s", buf);
        printf("Writing ...");
        write(fd, buf, strlen(buf)+1);
        printf("Done!\n");
        break;
      case '2':
        printf("Reading ...");
        read(fd, buf, 1024);
        printf("Done!\n\n");
        printf("Data = %s\n\n", buf);
        break;
      case '3':
        printf("Exiting ...");
        goto out;
      default:
        printf("Invalid Option: %c\n",option);
        break;
    }
  }

out:
  close(fd);
}